$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'monitor/bmonitorcamera/list',
        datatype: "json",
        colModel: [
            /*{ label: 'id', name: 'id', index: 'id', width: 50, key: true },*/
            { label: '仓库名称', name: 'wareHouseId', index: 'ware_house_id', width: 80 },
            { label: '设备出厂名称（海康高清红外球机）', name: 'name', index: 'name', width: 80 },
            { label: '别名（l例：1号仓球机）', name: 'alias', index: 'alias', width: 80 },
            { label: '账号', name: 'username', index: 'username', width: 80 },
            { label: '密码', name: 'password', index: 'password', width: 80 },
            { label: 'ip地址', name: 'ip', index: 'ip', width: 80 },
            { label: '端口', name: 'port', index: 'port', width: 80 },
            /*{ label: '子网掩码',hidden:true, name: 'subnetmask', index: 'subnetmask', width: 80 },
            { label: '网关',hidden:true, name: 'gateway', index: 'gateway', width: 80 }, */
            { label: '设备类型 2枪机 1球机', name: 'type', index: 'type', width: 80 },
            { label: '所属厂商 1-200海康 2-300大华 3其他', name: 'factory', index: 'factory', width: 80 },
            { label: '所在位置',hidden:true, name: 'live', index: 'live', width: 80 },
            { label: '所在位置', name: 'liveName', index: 'live_name', width: 80 },
            { label: '设备型号', name: 'modelNumber', index: 'model_number', width: 80 },
            /*{ label: '设备序列号',hidden:true, name: 'serialNumber', index: 'serial_number', width: 80 },
            { label: '安装位置',hidden:true, name: 'installationSite', index: 'installation_site', width: 80 },
            { label: '安装方式',hidden:true, name: 'installationMethod', index: 'installation_method', width: 80 },
            { label: '照射区域',hidden:true, name: 'irradiatedArea', index: 'Irradiated_area', width: 80 },
            { label: '取电位置',hidden:true, name: 'powerPosition', index: 'power_position', width: 80 },
            { label: '备注',hidden:true, name: 'remark', index: 'remark', width: 80 },
            { label: '通道号',hidden:true, name: 'channelNumber', index: 'channel_number', width: 80 },*/
            { label: '创建人id', name: 'createUserId', index: 'create_user_id', width: 80 },
            { label: '删除标志0删除，1未删除', name: 'isDel', index: 'is_del', width: 80 },
            { label: '创建时间', name: 'createTime', index: 'create_time', width: 80,formatter:function(value,options,row){
                    return new Date(value).Format('yyyy-MM-dd HH:mm:ss');} },
            { label: '修改时间', name: 'updateTime', index: 'update_time', width: 80,formatter:function(value,options,row){
                    return new Date(value).Format('yyyy-MM-dd HH:mm:ss');} },
            { label: '是否启用,0不启用，1启用', name: 'isUsed', index: 'is_used', width: 80 ,},
            { label: '过期时间', name: 'expireDate', index: 'expire_date', width: 80  }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "monitorCameraList.list",
            page: "monitorCameraList.currPage",
            total: "monitorCameraList.totalPage",
            records: "monitorCameraList.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bMonitorCamera: {},
        search:{//查询条件
            name:'',
        },
        locationList:[],
	},
    mounted:function(){
	    this.initList();
    },
	methods: {
        initList:function(){
            $.post(baseURL + "basics/blocation/getList/", {
                wareHouseId:localStorage.getItem("house"),
                systemId:localStorage.getItem("systemId")
            },function(r){
                vm.locationList = r.list;
            });
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bMonitorCamera = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.bMonitorCamera.id == null ? "monitor/bmonitorcamera/save" : "monitor/bmonitorcamera/update";
            var param = {
                "bMonitorCamera": vm.bMonitorCamera
            };
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(param),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "monitor/bmonitorcamera/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "monitor/bmonitorcamera/info/"+id, function(r){
                vm.bMonitorCamera = r.bMonitorCamera;
            });
		},
        reset:function(event) {
            vm.search = {
                name: '',
            }
            vm.reload();
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    name:vm.search.name,
                },
            }).trigger("reloadGrid");
		}
	}
});


Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}